package com.scheduler;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.common.constant.MailConstant;
import com.common.utils.DateUtil;
import com.common.utils.MailUtil;
import com.entity.MailText;
import com.service.MailTextService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Component
@Configurable
@EnableScheduling
public class InformSch {

    @Autowired
    private MailUtil mailUtil;

    @Autowired
    private MailTextService mailTextService;

    /**
     * 发送qq邮件
     */
//    @Scheduled(cron = "0 0/5 * * * *")
    public void sendMassage(){
        System.out.println("sendMassage");
        Wrapper<MailText> wr=new EntityWrapper<>();
        wr.eq("onoroff",MailText.ON);
        List<MailText> mailTexts = mailTextService.selectList(wr);
        if(CollectionUtils.isEmpty(mailTexts)){
            return;
        }
        for (MailText mailText : mailTexts) {
            Long time = mailText.getTime();
            //判断当前时间-
            String rTime = DateFormatUtils.format(new Date(time), "HHmm");
            String nowTime = DateFormatUtils.format(new Date(), "HHmm");
            int intOf= Integer.valueOf(nowTime)-Integer.parseInt(rTime);
            if(0<intOf&&intOf<=5){
                String text = mailText.getText();
                String title = mailText.getTitle();
                try {
//                    MailUtils.sendMail(MailConstant.FROM_QQ,MailConstant.FROM_QQ,title,text);
                    mailUtil.sendMail(MailConstant.FROM_QQ
                            , title+ DateUtil.getDate_yyyyMMdd(),
                            text
                    );
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
